/* eslint-disable no-unused-vars */
import React, { memo, useState, useEffect, useRef, useCallback } from "react";
import { useSelector, useDispatch, shallowEqual } from "react-redux";
import Taro from '@tarojs/taro';
import debounce from "lodash/debounce";
import { updateState } from "@/store/actions/global";
import { View, Image} from "@tarojs/components";
import { getUserInfo } from '@/utils/utils';
import {  imgUrl } from "@/utils/config";

import { Popup } from "@antmjs/vantui";
import "./index.less";

const Index = (props) => {
    const {canClose=true} = props
    const { globalData } = useSelector(
        (state) => ({ globalData: state.globalData }),
        shallowEqual
    );
    const { isLogin, isShowGrant } = globalData;
    const dispatch = useDispatch();
    const onClose = () => {
        if (!canClose) {
            return
        }
        dispatch(updateState({
            isShowGrant: false
        }))
    }
    const handleLogin = useCallback(
        debounce(async () => {
          console.log("login");
            const data = await getUserInfo();
            if (data) {
                const { userInfo } = data;
                const payload = {
                    userInfo,
                    isLogin: true,
                    isShowGrant:false
                }
                dispatch(updateState(payload));
            }
        },
            500,
            {
                leading: true,
                trailing: false,
            }
        ), []);

    return (
        <>
            <Popup
              show={isShowGrant}
              round
              closeable={canClose}
              onClose={onClose}
            >
                <View className='pop-authorization'>
                    <View className='content'>
                        <View className='logo'>
                            <Image src={imgUrl + 'logo.png'} mode='widthFix' />
                            <View className='btn-login' onClick={handleLogin}>授权登录</View>
                        </View>
                    </View>
                </View>
            </Popup>
        </>
    );
};

export default memo(Index);
